package cn.zjh.org.controller;

import cn.zjh.base.exception.BusinessException;
import cn.zjh.base.utils.ExcelUtil;
import cn.zjh.org.query.ShopQuery;
import cn.zjh.org.domain.ShopAuditLog;
import cn.zjh.org.service.IShopService;
import cn.zjh.org.domain.Shop;
import cn.zjh.base.utils.AjaxResult;
import cn.zjh.base.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop loadById(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> loadAll(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> queryData(@RequestBody ShopQuery query)
    {
        return shopService.queryData(query);
    }



    /**
    * @Title: settlement
    * @Description: 店铺入驻接口
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/1 20:30
    * @Parameters:
    * @Return
    */

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
    * @Title: pass
    * @Description: 通过接口
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/2 19:19
    * @Parameters:
    * @Return
    */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
    * @Title: reject
    * @Description: 驳回接口
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/2 19:20
    * @Parameters:
    * @Return
    */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }



    /**
     * @Title: reject
     * @Description: 拒绝接口
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/7/2 19:20
     * @Parameters:
     * @Return
     */
    @PostMapping("/audit/cancel")
    public AjaxResult cancel(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.cancel(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
     * @Title: exportExcel
     * @Description: 导出接口
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/7/2 20:20
     * @Parameters:
     * @Return
     */

    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse resp){
        //查询所有数据
        List<Shop> shops = shopService.loadAll();
        //调用工具类导出
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xls",resp);
    }



    /**
    * @Title: importExcel
    * @Description: 导入接口
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/2 20:56
    * @Parameters:
    * @Return
    */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        //批量新增@TODO
        System.out.println(shops);
    }


    /**
    * @Title: loadEchartsData
    * @Description: 报表数据接口
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/2 21:33
    * @Parameters:
    * @Return
    */
    @GetMapping("/echarts")
    public Map<String,Object> loadEchartsData(){
        Map<String, Object> stringObjectMap = shopService.loadEchartsData();
        return stringObjectMap;
    }



    @PostMapping("/logForm/{id}")
    public List<ShopAuditLog> logForm(@PathVariable("id") Long id)
    {
        return shopService.logForm(id);

    }

}
